<template>
    <el-dialog
            :title="!dataForm.phaseId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="阶段ID，主键" prop="phaseId">
                        <el-input v-model.number="dataForm.phaseId" placeholder="阶段ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="项目ID，外键关联项目基本信息表" prop="projectId">
                        <el-input v-model.number="dataForm.projectId" placeholder="项目ID，外键关联项目基本信息表"></el-input>
                </el-form-item>
                <el-form-item label="阶段名称" prop="phaseName">
                        <el-input v-model="dataForm.phaseName" placeholder="阶段名称"></el-input>
                </el-form-item>
                <el-form-item label="阶段开始日期" prop="phaseStartDate">
                        <el-date-picker
                                v-model="dataForm.phaseStartDate"
                                type="datetime"
                                placeholder="阶段开始日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="阶段结束日期" prop="phaseEndDate">
                        <el-date-picker
                                v-model="dataForm.phaseEndDate"
                                type="datetime"
                                placeholder="阶段结束日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="里程碑ID，唯一标识阶段中的里程碑" prop="milestoneId">
                        <el-input v-model.number="dataForm.milestoneId" placeholder="里程碑ID，唯一标识阶段中的里程碑"></el-input>
                </el-form-item>
                <el-form-item label="里程碑名称" prop="milestoneName">
                        <el-input v-model="dataForm.milestoneName" placeholder="里程碑名称"></el-input>
                </el-form-item>
                <el-form-item label="里程碑预计完成日期" prop="milestoneExpectedDate">
                        <el-date-picker
                                v-model="dataForm.milestoneExpectedDate"
                                type="datetime"
                                placeholder="里程碑预计完成日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="里程碑责任人" prop="milestoneResponsible">
                        <el-input v-model="dataForm.milestoneResponsible" placeholder="里程碑责任人"></el-input>
                </el-form-item>
                <el-form-item label="记录创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="记录创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="记录更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="记录更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="里程碑描述" prop="milestoneDescription">
                        <el-input v-model="dataForm.milestoneDescription" placeholder="里程碑描述"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            phaseId: null,
            projectId: null,
            phaseName: null,
            phaseStartDate: null,
            phaseEndDate: null,
            milestoneId: null,
            milestoneName: null,
            milestoneExpectedDate: null,
            milestoneResponsible: null,
            createdAt: null,
            updatedAt: null,
            milestoneDescription: null
          },
          dataRule: {
            phaseId: [
                        { required: true, message: '阶段ID，主键不能为空', trigger: 'blur' }
            ],
            projectId: [
                        { required: true, message: '项目ID，外键关联项目基本信息表不能为空', trigger: 'blur' }
            ],
            phaseName: [
                        { required: true, message: '阶段名称不能为空', trigger: 'blur' }
            ],
            phaseStartDate: [
                        { required: true, message: '阶段开始日期不能为空', trigger: 'blur' }
            ],
            phaseEndDate: [
                        { required: true, message: '阶段结束日期不能为空', trigger: 'blur' }
            ],
            milestoneId: [
                        { required: true, message: '里程碑ID，唯一标识阶段中的里程碑不能为空', trigger: 'blur' }
            ],
            milestoneName: [
                        { required: true, message: '里程碑名称不能为空', trigger: 'blur' }
            ],
            milestoneExpectedDate: [
                        { required: true, message: '里程碑预计完成日期不能为空', trigger: 'blur' }
            ],
            milestoneResponsible: [
                        { required: true, message: '里程碑责任人不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '记录创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '记录更新时间不能为空', trigger: 'blur' }
            ],
            milestoneDescription: [
                        { required: true, message: '里程碑描述不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.phaseId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.phaseId = id || 0
            if (this.dataForm.phaseId) {
              this.$http({
                url: this.$http.adornUrl(`/app/project_phase_milestone/info/${this.dataForm.phaseId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appprojectphasemilestone
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/project_phase_milestone/${!this.dataForm.phaseId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>